package com.example.cnotebook.fragment;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.cnotebook.CategoryActivity;
import com.example.cnotebook.R;
import com.example.cnotebook.RecycleActivity;
import com.example.cnotebook.SettingActivity;
import com.example.cnotebook.pojo.NoteInfo;
import com.example.cnotebook.pojo.UserInfo;
import com.example.cnotebook.util.OKHttpUtil;
import com.example.cnotebook.util.UserConstant;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UserFragment extends BaseFragment implements View.OnClickListener {
    private TextView tvUsername;
    private TextView tvAccount;
    private TextView tvNoteCount;
    private TextView tvDataCount;
    private TextView tvWordCount;
    private LinearLayout itemRecycle;
    private LinearLayout itemCategory;
    private LinearLayout itemSetting;
    private LinearLayout itemUpdate;

    private Context context;

    private Gson gson = new Gson();
    private SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_user_layout, null);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initView(view);

        try {
            initData();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    private void initData() throws ParseException {
        UserInfo userInfo = UserConstant.userInfo;
        tvUsername.setText(userInfo.getNickname());
        tvAccount.setText(userInfo.getAccount());

        tvNoteCount.setText(String.valueOf(UserConstant.noteInfos.size()));
        Date registerDate = UserConstant.userInfo.getRegisterDate();
        registerDate = format.parse(format.format(registerDate));
        long time = registerDate.getTime();
        long time1 = format.parse(format.format(new Date())).getTime();
        int between = (int) (time1 - time) / (1000 * 60 * 60 * 24);
        tvDataCount.setText(String.valueOf(between));
        int i = 0;
        for (NoteInfo noteInfo : UserConstant.noteInfos) {
            i += noteInfo.getSize();
        }
        tvWordCount.setText(String.valueOf(i));
    }

    private void initView(View view) {
        tvUsername = view.findViewById(R.id.tv_username);
        tvAccount = view.findViewById(R.id.tv_account);
        itemRecycle = view.findViewById(R.id.item_recycle);
        itemCategory = view.findViewById(R.id.item_category);
        itemSetting = view.findViewById(R.id.item_setting);
        itemUpdate = view.findViewById(R.id.item_update);
        tvNoteCount = view.findViewById(R.id.tv_noteCount);
        tvDataCount = view.findViewById(R.id.tv_dateCount);
        tvWordCount = view.findViewById(R.id.tv_wordCount);

        context = getContext();

        itemCategory.setOnClickListener(this);
        itemRecycle.setOnClickListener(this);
        itemSetting.setOnClickListener(this);
        itemUpdate.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.item_recycle:
                startActivity(new Intent(context, RecycleActivity.class));
                break;
            case R.id.item_category:
                startActivity(new Intent(context, CategoryActivity.class));
                break;
            case R.id.item_setting:
                startActivity(new Intent(context, SettingActivity.class));
                break;
            case R.id.item_update:
                Toast.makeText(context, "已是最新版！", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private void queryNote() {
        OKHttpUtil.get("/note/list/" + UserConstant.userInfo.getId(), new OKHttpUtil.CallBack() {
            @Override
            public void onSuccess(JSONObject response) throws JSONException {
                JSONArray jsonArray = response.getJSONArray("data");
                UserConstant.noteInfos.clear();
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    UserConstant.noteInfos.add(gson.fromJson(jsonObject.toString(), NoteInfo.class));
                }
                try {
                    initData();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {

            }
        }, (Activity) context);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (UserConstant.userIsSaveData) {
            queryNote();
            UserConstant.userIsSaveData = false;
        }
    }
}
